﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;

namespace WindowsApplication1
{
    public partial class Form1 : RibbonForm
    {

        IMap pMap;
        IActiveView pActiveView;
        bool flag = false;
        bool flag2 = false;

        public Form1()
        {
            InitializeComponent();
            InitSkinGallery();

            
        }
        void InitSkinGallery()
        {
            SkinHelper.InitSkinGallery(rgbiSkins, true);
        }


        private void iOpen_ItemClick(object sender, ItemClickEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openFileDialog;
            openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "打开地图文档";
            openFileDialog.Filter = "map documents(*.mxd)|*.mxd";
            openFileDialog.ShowDialog();
            string filePath = openFileDialog.FileName;

            if (axMapControl1.CheckMxFile(filePath))
            {
                axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                axMapControl1.LoadMxFile(filePath, 0, Type.Missing);
                axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
            }
            else
            {
                MessageBox.Show(filePath + "不是一个有效的地图文档！！！");
            }


        }

        private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e)
        {
            IPoint pointInMap;
            pointInMap = axMapControl1.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x,e.y);
            string MapStatusX = pointInMap.X.ToString();
            string MapStatusY = pointInMap.Y.ToString();
            siStatus.Caption = "鼠标坐标为：" + MapStatusX +"   "+ MapStatusY;


            if (flag2 == true)
            {
                panMap();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pMap = axMapControl1.Map;
            pActiveView = pMap as IActiveView;
            pActiveView.Extent = axMapControl1.FullExtent;
            pActiveView.Refresh();
        }

        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            if (flag == true)
            {
                flag2 = true;
                //rectangleZoomIn();
            }
            else 
            {
                flag2 = false;
            }
        }

        private void btnZoomIn_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (flag == false)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
        }

        private void btnFullMap_ItemClick(object sender, ItemClickEventArgs e)
        {
            IEnvelope pEnv=pActiveView.FullExtent;
            pActiveView.Extent = pEnv;
            pActiveView.Refresh();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            ESRI.ArcGIS.SystemUI.ICommand pCommand;
            pCommand = new ESRI.ArcGIS.Controls.ControlsSelectFeaturesToolClass();
            pCommand.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = pCommand as ESRI.ArcGIS.SystemUI.ITool;
        }

        private void rectangleZoomIn()
        {
            axMapControl1.MousePointer = ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerCrosshair;
            IEnvelope pEnv;
            pEnv = axMapControl1.TrackRectangle();
            pActiveView.Extent = pEnv;
            pActiveView.Refresh();
        }
        private void panMap()
        {
            ESRI.ArcGIS.SystemUI.ICommand pCommand;
            pCommand = new ESRI.ArcGIS.Controls.ControlsMapPanToolClass();
            pCommand.OnCreate(axMapControl1.Object);
            axMapControl1.CurrentTool = pCommand as ESRI.ArcGIS.SystemUI.ITool;
            pCommand.OnClick();
        }





    }
}